<?php
class M_product extends CI_Model {
    function __construct() {
        parent::__construct();
    }
    
    function get_count($data) {
        $this->db->from('product');
        $this->db->select('count(1) as cnt');
        $this->db->where($data);
        $result = $this->db->get()->row_array();
        return $result['cnt'];
    }
    
    function get_product($data) {
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        $this->db->from('product');
        if (!isset($data['productid'])) {
            return false;
        }
        $this->db->where('productid', $data['productid']);
        return $this->db->get()->row_array();
    }
    
    function get_product_in_id($id_list){
        $list= array();
        foreach($id_list as $k =>$v){
            $v = intval($v);
            if($v > 1){
                $list[]=$v;
            }
        }        
        if(empty($list)){
            return null;
        }
        $this->db->where_in('productid',$list);
        return $this->db->get('product')->result_array();
    }

    function get_product_list($data) {
        $this->db->from('product');
        if (isset($data['limit'])) {
            $this->db->limit($data['limit'], $data['offset']);
        }
        if (isset($data['where'])) {
            if(isset($data['where']['productname'])){
                $this->db->like('productname',$data['where']['productname']);
                unset($data['where']['productname']);
            }
            $this->db->where($data['where']);
        }
        if (isset($data['order'])) {
            $this->db->order_by($data['order']);
        }
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    function insert($data) {
        $this->db->insert('product', $data);
    }

    function update($data) {
        if (!isset($data['productid'])) {
            return false;
        }
        $this->db->where('productid', $data['productid']);
        unset($data['productid']);
        return $this->db->update('product', $data);
    }

    function delete($data) {
        return $this->db->delete('product', $data);
    }

}
